<template>
	<view class="container">
		<!-- 顶部搜索栏 -->
		<view class="search">
			<view class="search-con">
				<view class="search-con-keyword">
					<input class="search-con-keyword-inp" type="text" v-model="searchKeyword"
						placeholder="输入物料编码、物料名称或型号" @input="searchChange" :auto-focus="showFocus" />
					<view class="search-con-keyword-submit" @click="search()">
						<text class="search-keyword-submit-text">搜索</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 取料列表 -->
		<view class="inventory-list">
			<!-- 固定表头 -->
			<view class="list-header">
				<text class="header-item" style="width: 13%">货道</text>
				<text class="header-item" style="width: 14%">待回收</text>
				<text class="header-item" style="width: 30%">名称</text>
				<text class="header-item" style="width: 28%">取料时间</text>
				<text class="header-item" style="width: 15%">操作</text>
			</view>

			<!-- 可滚动内容区域 -->
			<scroll-view class="list-content" scroll-y="true" :style="{height: scrollHeight + 'px'}"
				@scrolltolower="loadMore">
				<view class="list-item" v-for="(item, index) in filteredList" :key="index">
					<text class="item-column location" style="width: 13%">{{ item.identification || '-' }}</text>
					<text class="item-column stock" style="width: 14%">{{ item.opnum || 0 }}</text>
					<text class="item-column name" style="width: 30%">{{ item.material_name || '-' }}</text>
					<text class="item-column time" style="width: 28%">{{ item.create_time }}</text>
					<view class="item-column action" style="width: 15%">
						<button class="action-btn" @click="handleAdjust(item)">回收</button>
					</view>
				</view>
			</scroll-view>
		</view>

		<info-popup :material="selectMaterial" :recoveryStoreList="recoveryStoreList" @recoveryFinish="recoveryFinish"
			@closeInfoPopup="closeInfoPopup" :isWeightCabinet="isWeightCabinet"
			@updateRecoveryStoreList="getRecoveryStoreList" v-if="infoPopupShow"></info-popup>
	</view>
</template>

<script>
	const API = require("../../utils/apiConfig.js");
	import infoPopup from './components/info-popup.vue';
	export default {
		components: {
			infoPopup
		},
		data() {
			return {
				searchKeyword: '',
				showFocus: false,
				pageIndex: 1,
				pageSize: 20,
				total: 0,
				searchKeyword: '',
				list: [],
				infoPopupShow: false,
				selectMaterial: {}, //选择的物料
				recoveryStoreList: [], //回收存放货道
				scrollHeight: 0, // 滚动区域高度
				loading: false, // 上拉加载状态
				noMore: false, // 是否没有更多数据
				refreshing: false, // 下拉刷新状态
			}
		},
		computed: {
			// 过滤后的列表
			filteredList() {
				if (!this.searchKeyword) return this.list;
				const keyword = this.searchKeyword.toLowerCase();
				return this.list.filter(item =>
					(item.material_name && item.material_name.toLowerCase().includes(keyword)) ||
					(item.identification && item.identification.toLowerCase().includes(keyword))
				);
			}
		},
		onLoad() {
			// 计算滚动区域高度
			this.calculateScrollHeight();
		},
		onShow() {
			this.recovery("refresh");
			this.getRecoveryStoreList();
		},
		methods: {
			// 计算滚动区域高度
			calculateScrollHeight() {
				const query = uni.createSelectorQuery().in(this);
				query.select('.inventory-list').boundingClientRect(data => {
					const systemInfo = uni.getSystemInfoSync();
					const windowHeight = systemInfo.windowHeight;
					const searchHeight = 72; // 搜索栏高度
					const headerHeight = 44; // 表头高度
					this.scrollHeight = windowHeight - searchHeight - headerHeight - 30; // 30是额外边距
				}).exec();
			},

			// 搜索输入变化
			searchChange() {
				// 可以添加防抖处理
				this.recovery("refresh")
			},
			recoveryFinish(returnId, takelogId, num) {

			},
			recovery(type) {
				if (type == 'refresh') {
					this.pageIndex = 1;
					this.noMore = false;
				}
				if (this.loading || this.noMore) return;

				this.loading = true;
				API.recovery({
					page: this.pageIndex,
					rows: this.pageSize,
					search: this.searchKeyword
				}).then((res) => {
					console.log('res   ' + JSON.stringify(res));
					if (res.code == 0) {
						if (type == 'refresh') {
							this.list = res.data.list || [];
						} else {
							this.list = [...this.list, ...(res.data.list || [])];
						}
						this.total = res.data.total || 0;
						if (res.data.list && res.data.list.length < this.pageSize) {
							this.noMore = true;
						} else {
							this.noMore = false;
						}
						// 如果是刷新操作，重置页码
						if (type === 'refresh') {
							this.pageIndex = 1;
						}
					}
				}).finally(() => {
					this.loading = false;
					this.refreshing = false;
				})
			},
			getRecoveryStoreList() {
				API.recoverycargo({
					all: 0
				}).then((res) => {
					if (res.code == 0) {
						let recoveryStoreList = res.data;
						for (let i = 0; i < recoveryStoreList.length; i++) {
							recoveryStoreList[i].text = recoveryStoreList[i].identification;
							recoveryStoreList[i].value = JSON.stringify(recoveryStoreList[i]);
						}
						this.recoveryStoreList = recoveryStoreList;
						console.log('回收存放货道==', this.recoveryStoreList);
					}
				})
				this.recovery("refresh");
			},
			// 执行搜索
			search() {
				// 这里可以调用API获取数据
				console.log("搜索关键词:", this.searchKeyword);
				this.recovery("refresh")
			},

			// 调整库存
			handleAdjust(item) {
				this.selectMaterial = item;
				this.infoPopupShow = true;
			},
			closeInfoPopup() {
				this.infoPopupShow = false;
			},
			// 确认提交
			handleConfirm() {
				// 获取所有修改过的数据
				const changedItems = this.list.filter(item => item._modified);
				console.log("提交的数据:", changedItems);

				uni.showLoading({
					title: '提交中...'
				});
				// 模拟API请求
				setTimeout(() => {
					uni.hideLoading();
					uni.showToast({
						title: '提交成功',
						icon: 'success'
					});
				}, 1000);
			},
			loadMore() {
				if (this.loading || this.noMore) return;
				this.pageIndex++;
				this.recovery('loadmore');
			},
			// 加载数据
			loadData() {
				uni.showLoading({
					title: '加载中...'
				});

				// 模拟数据加载
				setTimeout(() => {
					uni.hideLoading();
				}, 500);
			}
		}
	}
</script>

<style>
	.container {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: #f5f5f5;
	}

	/* 搜索框区域 */
	.search {
		padding: 20rpx;
		background-color: #fff;
		box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.05);
	}

	.search-con {
		width: 100%;
		display: flex;
		align-items: center;
	}

	.search-con-keyword {
		display: flex;
		width: 100%;
		background-color: #f2f2f2;
		border-radius: 40rpx;
		overflow: hidden;
	}

	.search-con-keyword-inp {
		flex: 1;
		height: 72rpx;
		padding: 0 20rpx;
		font-size: 28rpx;
		color: #333;
	}

	.search-con-keyword-submit {
		width: 120rpx;
		height: 72rpx;
		line-height: 72rpx;
		text-align: center;
		background-color: #07c160;
		color: #fff;
		font-size: 28rpx;
	}

	.search-keyword-submit-text {
		font-weight: 500;
	}

	/* 列表样式 */
	.inventory-list {
		flex: 1;
		background-color: #fff;
		border-radius: 8px;
		overflow: hidden;
		box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
		margin: 15rpx;
		display: flex;
		flex-direction: column;
	}

	.list-header {
		display: flex;
		background-color: #333847;
		padding: 12px 0;
	}

	.header-item {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		text-align: center;
		font-size: 14px;
		color: #fff;
		padding: 0 5px;
	}

	.list-content {
		flex: 1;
		overflow-y: auto;
	}

	.list-item {
		display: flex;
		padding: 12px 0;
		border-bottom: 1px solid #f0f0f0;
		align-items: center;
	}

	.item-column {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		text-align: center;
		font-size: 14px;
		padding: 0 5px;
		word-break: break-word;
	}

	.name {
		color: #333;
	}

	.location {
		color: #666;
	}

	.stock {
		color: #333;
	}

	.time {
		color: #666;
		white-space: normal;
		word-break: break-all;
		line-height: 1.4;
		padding: 5px;
	}

	.action-btn {
		width: 80rpx;
		height: 50rpx;
		line-height: 50rpx;
		padding: 0;
		font-size: 24rpx;
		background-color: #333847;
		color: #fff;
		border-radius: 8rpx;
	}
</style>